package com.kangyi.admin.controller;

import com.kangyi.admin.service.ReviewService;
import com.kangyi.dto.AddReviewReplyDTO;
import com.kangyi.dto.ReviewPageDTO;
import com.kangyi.dto.ReviewPageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.ReviewDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/review")
@Slf4j
@Api(tags = "养老院-养老院评论管理相关接口")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @GetMapping("/page")
    @ApiOperation("评论管理分页查询")
    public Result<PageResult> pageQuery(ReviewPageDTO reviewPageDTO) {
        log.info("评论管理分页查询：{}",reviewPageDTO);
        try {
            PageResult pageResult = reviewService.pageQuery(reviewPageDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.success(e.getMessage());
        }
    }

    @PostMapping("/reply")
    @ApiOperation("回复评论")
    @CacheEvict(cacheNames = "review_", allEntries = true)
    public Result replyReview(@RequestBody AddReviewReplyDTO addReviewReplyDTO){
        log.info("回复评论:{}",addReviewReplyDTO);
        try {
            reviewService.replyReview(addReviewReplyDTO);
            return Result.success("回复成功！");
        } catch (Exception e) {
            return Result.success(e.getMessage());
        }
    }

    @GetMapping("/detail/{reviewId}")
    @ApiOperation("查询评论详情")
//    @Cacheable(cacheNames = "review_", key = "'adminDetail_'+#reviewId")
    public Result<ReviewDetailVO> detailReview(@PathVariable Integer reviewId){
        log.info("查询评论详情：{}",reviewId);
        try {
            ReviewDetailVO reviewDetailVO = reviewService.detailReview(reviewId);
            return Result.success(reviewDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.success(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{replyId}")
    @ApiOperation("删除回复")
    @CacheEvict(cacheNames = "review_", allEntries = true)
    public Result deleteReply(@PathVariable Integer replyId){
        log.info("删除回复{}",replyId);
        try {
            reviewService.deleteReply(replyId);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.success(e.getMessage());
        }
    }
}
